﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Press.MVC.Data;
using System.Web.Security;

namespace Press.MVC.Services
{
    public class BlogService : IBlogService
    {
        IBlogRepository _repository = null;
        /// <summary>
        /// Creates a BlogService based on the passed-in repository
        /// </summary>
        /// <param name="repository">An IBlogRepository</param>
        public BlogService(IBlogRepository repository) {
            _repository = repository;
            if (_repository == null)
                throw new InvalidOperationException("Repository cannot be null");
        }

        public IList<Blog> GetBlogs()
        {
            IList<Blog> blogs = _repository.GetBlogs().ToList();           
            return blogs;
        }

        public Blog GetBlog(int blogId)
        { 
            // Get the Blog
            Blog b = _repository.GetBlogs().WithBlogId(blogId).SingleOrDefault();

            return b;
        }

     
        public Blog GetBlog(string shortName)
        {
            Blog b = _repository.GetBlogs().WithBlogShortName(shortName).SingleOrDefault();
            return b;
        }

        public bool SaveBlog(Blog blog)
        {
            if (blog != null)
            {
                _repository.SaveBlog(blog);
            }

            return true;
        }

        public Post GetPost(int postId)
        {
            // Get the Post
            Post p = _repository.GetPosts().WithPostId(postId).SingleOrDefault();
            return p;            
        }

        public PostCategory GetPostCategory(int postCategoryId)
        {
            PostCategory pc = _repository.GetPostCategories().WithPostCategoryId(postCategoryId).SingleOrDefault();
            return pc;
        }

        public IList<PostCategory> GetPostCategories(int blogId)
        {
            IList<PostCategory> pc = _repository.GetPostCategories().WithBlogId(blogId).ToList();
            return pc;
        }

        /// <summary>
        /// Saves the post to the repository
        /// </summary>
        public void SavePost(Post post)
        {
            if (post != null)
            {
                _repository.SavePost(post);
            }
        }

        public void DeletePost(int postId)
        {
            _repository.DeletePost(postId);
        }

        public void SavePostCategory(PostCategory pc)
        {
            if (pc != null)
            {
                _repository.SavePostCategory(pc);
            }
        }

        //TODO
        //public void DeletePostCategory(int postCategoryId)
        //{
        //    _repository.DeletePostCategory(postCategoryId);
        //}

        public void SavePostComment(PostComment pc)
        {
            if (pc != null)
            {
                _repository.SavePostComment(pc);
            }
        }

        

        //public PostComment GetPostComment(int id)
        //{ 
            
        //}

        //public IList<Post> GetPostsByBlog(int blogId)
        //{ 
            
        //}

        //public IList<PostComment> GetPostCommentsByPost(int postId)
        //{ 
            
        //}

    //    GetPostComment(int postId);

    //    System.Collections.Generic.IList<Press.MVC.Data.Post> GetPostsByBlog(int blogId);
    //    System.Collections.Generic.IList<Press.MVC.Data.PostComment> GetPostCommentsByPost(int postId);
    //}

    }
}
